package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Contact;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class ContactDao {
	
	private DBHelp<Contact> db = new DBHelp<Contact>();
	
	public Contact FindByName (String id)	{
		String sql = "select id,name,companyname,mobile,tel,email,address,website,weibo,desc,createtime from t_contact where id = ?";
		return db.executeQueryForObject(sql, new ContactRowMapper(), id);
	}
	
	public List<Contact> FindAll () {
		String sql = "select name,companyname,mobile,tel,email,address,website,weibo,desc,createtime from t_contact ";
		return db.executeQueryForList(sql, new ContactRowMapper());
	}
	
	public void Save (String id,String name,String companyname,String mobile,String tel,String email,String address,String website,String weibo,String desc,String createtime) {
		String sql = "insert into t_contact(id,name,companyname,mobile,tel,email,address,website,weibo,desc,createtime) values(?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql,id,name,companyname,mobile,tel,email,address,website,weibo,desc,createtime);
	}
	
	public void Update (Contact contact) {
		String sql = "update t_contact set name = ?,companyname = ?,mobile = ?,tel = ?,email = ?,address = ?,website = ?,weibo = ?,desc = ?,createtime = ? where id = ?";
		db.executeSQL(sql,contact.getName(),contact.getCompanyname(),contact.getMobile(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getWebsite(),contact.getWeibo(),contact.getDesc(),contact.getCreatetime(),contact.getId());
	}
	
	public void Del (String id) {
		String sql = "delete from t_contact where id = ?";
		db.executeSQL(sql, id);
	}
	private class ContactRowMapper implements RowMapper<Contact> {
		
		public Contact mapperRow (ResultSet rs) throws SQLException {
			
			Contact contact = new Contact();
			contact.setId(rs.getString("id"));
			contact.setName(rs.getString("name"));
			contact.setCompanyname(rs.getString("companyname"));
			contact.setMobile(rs.getString("mobile"));
			contact.setTel(rs.getString("tel"));
			contact.setEmail(rs.getString("email"));
			contact.setAddress(rs.getString("address"));
			contact.setWebsite(rs.getString("website"));
			contact.setWeibo(rs.getString("weibo"));
			contact.setDesc(rs.getString("desc"));
			contact.setCreatetime(rs.getString("createtime"));
			return contact;
		}
	}
}
